importPackage(java.lang)
importPackage(com.ym.script)
importPackage(com.ym.uiautomator2)
importPackage(com.ym.script.util)
importPackage(com.ym.script.command)
importPackage(java.util.regex)

var device = UiDevice.getInstance()
function include(file){
    load(getDir()+file)
}
function sleep(time){
    Thread.sleep(time)
}
function time(){
    return System.currentTimeMillis();
}
function findView(res){
    return device.findObject(res)
}
function findViews(res){
    return device.findObjects(res)
}
function wait(func,timeout){
    var endtime = time()+timeout
    while(time()<endtime){
        return func()
        sleep(1000)
    }
}
function waitView(res,timeout){
    return wait(function(){
        var view = findView(res)
        if(view!=null){
            return view
        }
    },timeout)
}
function waitActivity(name,timeout){
    return wait(function(){
        var top = device.getTopActivity()
        if(top.contains(name)){
            return top;
        }
    },timeout)
}
function input(text){
    Am.run("broadcast -a com.ym.script.service.AutoInputService --es input "+text)
}
function pattern(re){
    return Pattern.compile(re)
}